package com.wonders.module.demo.dao.impl;

import com.wonders.framework.dao.impl.BaseDaoImpl;
import com.wonders.framework.entity.vo.Page;
import com.wonders.framework.entity.vo.Parameter;
import com.wonders.framework.util.DateUtil;
import com.wonders.module.demo.dao.DemoDao;
import com.wonders.module.demo.entity.bo.Demo;
import com.wonders.module.demo.entity.vo.DemoVo;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class DemoDaoImpl extends BaseDaoImpl implements DemoDao {

	public Page query(DemoVo demoVo, Page page) {
		Criteria criteria = super.getCurrentSession()
				.createCriteria(Demo.class);
		if (StringUtils.isNotBlank(demoVo.getTitle())) {
			criteria.add(Restrictions.like("title", "%" + demoVo.getTitle()
					+ "%"));
		}
		if (demoVo.getType() != null) {
			criteria.add(Restrictions.eq("type", demoVo.getType()));
		}
		if (demoVo.getTime() != null) {
			criteria.add(Restrictions.between("time", demoVo.getTime(),
					DateUtil.getDateByDays(demoVo.getTime(), 1)));
		}
		criteria.addOrder(Order.desc("time"));
		return super.pageByCriteria(criteria, page);
	}

	public void remove(List<Long> keys) {
		String hql = "delete from Demo d where d.id in (:keys)";
		List<Parameter> args = new ArrayList<Parameter>();
		args.add(new Parameter("keys", keys));
		super.excuteHql(hql, args);
	}

}